/**
 * @Date 2025/2/26 13:24
 * @Created by 衡盛永
 */

package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.CategoryPageVO;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealPageVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 套餐业务层
 */
@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        int page = setmealPageQueryDTO.getPage();
        int pageSize = setmealPageQueryDTO.getPageSize();
        PageHelper.startPage(page, pageSize);
        Page<Setmeal> setmeals = setmealMapper.pageQuery(setmealPageQueryDTO);
        ArrayList<SetmealPageVO> setmealPageVOArrayList = new ArrayList<>();
        for (Setmeal setmeal : setmeals) {
            SetmealPageVO setmealPageVO = new SetmealPageVO();
            Long categoryId = setmeal.getCategoryId();
            // 查询套餐菜品分类名称
            String categoryName = categoryMapper.getCategoryNameById(categoryId);
            BeanUtils.copyProperties(setmeal, setmealPageVO);
            setmealPageVO.setCategoryName(categoryName);
            setmealPageVOArrayList.add(setmealPageVO);
        }
        PageInfo<SetmealPageVO> setmealPageVOPageInfo = new PageInfo<>(setmealPageVOArrayList);
        PageResult pageResult = new PageResult();
        pageResult.setTotal(setmealPageVOPageInfo.getTotal());
        pageResult.setRecords(setmealPageVOPageInfo.getList());
        return pageResult;
    }

    @Override
    public void addSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmeal.setStatus(StatusConstant.ENABLE);
        setmeal.setCreateTime(LocalDateTime.now());
        setmeal.setUpdateTime(LocalDateTime.now());
        setmeal.setCreateUser(BaseContext.getCurrentId());
        setmeal.setUpdateUser(BaseContext.getCurrentId());
        setmealMapper.addSetmeal(setmeal);
        Long setmealId = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            SetmealDish setmealAndDish = new SetmealDish();
            BeanUtils.copyProperties(setmealDish, setmealAndDish);
            setmealAndDish.setSetmealId(setmealId);
            setmealDishMapper.saveSetmealDish(setmealAndDish);
        }
    }

    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        return setmealDishMapper.list(setmeal);
    }

    @Override
    public List<DishItemVO> dishesBySetmealId(Long setmealId) {
        return setmealMapper.dishesBySetmealId(setmealId);
    }
}
